<?php

$dsthi=$_GET['ds_thi'];//'HD0134081113084747';
$sql_tt_dsthi=" SELECT  khungthoigian,ten_mh,ten_nganh,ngaythi,tbl_dsthi.ma_mon_ctdt,ten_tt_diemthi,trangthai_dsthi 
                FROM    tbl_dsthi,dm_cahoc,tbl_mon_ctdt,dm_monhoc,tbl_ctdt,dm_nganh,dm_trangthai_diemthi
                WHERE   tbl_dsthi.madm_ca=dm_cahoc.madm_ca
                AND     tbl_dsthi.ma_mon_ctdt=tbl_mon_ctdt.ma_mon_ctdt
                AND     tbl_ctdt.ma_ctdt=tbl_mon_ctdt.ma_ctdt
                AND     tbl_mon_ctdt.ma_monhoc=dm_monhoc.madm_mh
                AND     tbl_ctdt.madm_nganh=dm_nganh.madm_nganh
                AND     tbl_dsthi.trangthai_dsthi=dm_trangthai_diemthi.madm_tt_diemthi
                AND     ma_dsthi='$dsthi'";
                 
$ex_tt_dslich=mysql_query($sql_tt_dsthi);
$kq=mysql_fetch_array($ex_tt_dslich);                

$smarty->assign('kq_tt_ds',$kq);
$smarty->assign('dsthi',$dsthi);
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4
if(isset($_POST['Luu'])){
    $ma_mon_ctdt=$_POST['ma_mon_ctdt'];
    $ma_dvhv='1-2013';
    $somax=$_POST['somax'];
    $ngaygio=date('dmyhis');
    $thoigian=date('d/m/Y');
    $ma_cb=$_SESSION['ma_cb'];
    for($i=1;$i<$somax;$i++){
        $ma_sv=$_POST['ma_sv'.$i];
        $lanthi=$_POST['lanthi'.$i];
        $diemcuoi=$_POST['diemcuoi'.$i];
        $ma_diemthi=$_POST['ma_diemthi'.$i];
        
        $ma_diem_dvhv=$ma_sv.$ma_mon_ctdt.$ngaygio.$i;
        
        //Thêm bản ghi vào điểm đơn vị học vụ
        $sql_xacnhan="INSERT INTO  tbl_bangdiem_donvihocvu ( sv_diem_dvhv ,  ma_donvihocvu ,  ma_sv ,  ma_mon_ctdt ,  lanthi ,  diem )
                                                     VALUES ('$ma_diem_dvhv','$ma_dvhv','$ma_sv','$ma_mon_ctdt','$lanthi','$diemcuoi')";
        mysql_query($sql_xacnhan);        
        
        //cập nhật trạng thái thông tin điểm đã duyệt
        $sql_diemthi="UPDATE  tbl_diemthi  SET  nguoixacnhan ='$ma_cb', thoigianxacnhan ='$thoigian', ma_tt_diemthi ='daduyet' WHERE ma_diemthi ='$ma_diemthi'";
        mysql_query($sql_diemthi);
    }    
    //Cập nhật thông tin danh sách thi với trạng thái là đã duyệt
    $sql_update_dsthi="UPDATE tbl_dsthi SET trangthai_dsthi='daduyet' WHERE ma_dsthi='$dsthi'";
     mysql_query($sql_update_dsthi);
}


//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4
$sql_sv_lopmon="    SELECT  tbl_dangkymon.*,hoten_sv,ngaysinh_sv,ten_lop,ma_diemthi,lanthi,diemthi
                    FROM    tbl_dangkymon,tbl_sinhvien,tbl_nhaphoc,tbl_sv_lop,tbl_lop,tbl_diemthi
                    WHERE   tbl_dangkymon.ma_sv=tbl_sinhvien.ma_sv
                    AND     tbl_sinhvien.ma_sv=tbl_nhaphoc.ma_sv
                    AND     tbl_nhaphoc.ma_nhaphoc=tbl_sv_lop.ma_nhaphoc
                    AND     tbl_sv_lop.ma_lop=tbl_lop.ma_lop
                    AND     tbl_diemthi.ma_dkm=tbl_dangkymon.ma_dkm
                    AND     ngayra_nhaphoc = ''
                    AND     ma_dsthi='$dsthi'
                    ORDER BY ten_sv,hoten_sv ASC";
$ds_sv=null;
$ds_dkm=null;
$ex_sv_lopmon=mysql_query($sql_sv_lopmon);
while($row=mysql_fetch_array($ex_sv_lopmon)){
    $ds_sv[]=$row;
    $ds_dkm.="'".$row['ma_dkm']."',";
}

$smarty->assign('dssv',$ds_sv);
$ds_dkm=rtrim($ds_dkm,",");
                           
$ex_check_cc=mysql_query("  SELECT  tbl_ketquaquatrinh.ma_dkm,diem_kqqt,trongso,mienmax,ma_kqqt 
                            FROM    tbl_dangkymon,tbl_ketquaquatrinh,tbl_monctdt_tieuchi
                            WHERE   tbl_dangkymon.ma_dkm=tbl_ketquaquatrinh.ma_dkm
                            AND     tbl_monctdt_tieuchi.ma_monctdt_tieuchi=tbl_ketquaquatrinh.ma_monctdt_tieuchi
                            AND     madm_tieuchimonhoc='cc'
                            AND     tbl_dangkymon.ma_dkm IN ($ds_dkm)");


$arr_kqqt_cc=array();
$arr_kqqt_dk=array();
$arr_kqqt_tongdiem=array();
$arr_kqqt_cc=null;
$arr_kqqt_dk=null;
$arr_kqqt_tongdiem=null;


if(mysql_num_rows($ex_check_cc)>0){
    while($row=mysql_fetch_array($ex_check_cc)){
        $key=$row['ma_dkm'];
        $arr_kqqt_cc[$key]=$row['diem_kqqt'];        
    }
    
        $ex_check_dk=mysql_query(" SELECT  tbl_ketquaquatrinh.ma_dkm,diem_kqqt,trongso,mienmax,ma_kqqt 
                                    FROM    tbl_dangkymon,tbl_ketquaquatrinh,tbl_monctdt_tieuchi
                                    WHERE   tbl_dangkymon.ma_dkm=tbl_ketquaquatrinh.ma_dkm
                                    AND     tbl_monctdt_tieuchi.ma_monctdt_tieuchi=tbl_ketquaquatrinh.ma_monctdt_tieuchi
                                    AND     madm_tieuchimonhoc='dk'
                                    AND     tbl_dangkymon.ma_dkm IN ($ds_dkm)");
        
        if(mysql_num_rows($ex_check_dk)>0){
            while($row=mysql_fetch_array($ex_check_dk)){
                $key=$row['ma_dkm'];
                $arr_kqqt_dk[$key]=$row['diem_kqqt'];   
            }
        }
        
        
//Tính điểm tổng của sinh viên    
        //Lấy tham số    
        $sql_thamso="SELECT tbl_dsthi.ma_mon_ctdt,madm_tieuchimonhoc,trongso,mienmax 
                        FROM tbl_dsthi,tbl_mon_ctdt,tbl_monctdt_tieuchi 
                        WHERE tbl_dsthi.ma_mon_ctdt=tbl_mon_ctdt.ma_mon_ctdt 
                        AND tbl_mon_ctdt.ma_mon_ctdt=tbl_monctdt_tieuchi.ma_mon_ctdt 
                        AND ma_dsthi='$dsthi'";
        $ex_thamso=mysql_query($sql_thamso);
        while($row=mysql_fetch_array($ex_thamso)){
            $tieuchi=$row['madm_tieuchimonhoc'];
            $trongso[$tieuchi."_ts"]=$row['trongso'];
            $thamso[$tieuchi."_thamso"]=10/$row['mienmax'];
        }
        
        //tính điểm
        foreach($ds_sv as $key=>$value){
            $dkm=$value['ma_dkm'];
            $diemcc=$arr_kqqt_cc[$dkm]*$thamso['cc_thamso']*$trongso["cc_ts"];
            $diemdk=$arr_kqqt_dk[$dkm]*$thamso['dk_thamso']*$trongso["dk_ts"];
            $diemthi=$value['diemthi']*(1-$trongso["cc_ts"]-$trongso["dk_ts"]);
            $arr_kqqt_tongdiem[$dkm]=$diemcc+$diemdk+$diemthi;          
        }        
}


$smarty->assign('arr_kqqt_cc',$arr_kqqt_cc);
$smarty->assign('arr_kqqt_dk',$arr_kqqt_dk);
$smarty->assign('arr_kqqt_tongdiem',$arr_kqqt_tongdiem);
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$smarty->display('diem/xacnhandiem.tpl');   
?>












